package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.mapper.OrderBasicMapper;
import org.example.model.common.dtos.PageResponseDto;
import org.example.model.common.dtos.dish.DishBasicDto;
import org.example.model.common.dtos.order.OrderBasicDto;
import org.example.model.common.vos.order.SearchOrderVo;
import org.example.model.money.MoneyTotal;
import org.example.model.order.OrderBasic;
import org.example.service.AddressBookService;
import org.example.service.MoneyTotalService;
import org.example.service.OrderBasicService;
import org.example.service.UserBasicService;
import org.example.utils.PageUtils;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单管理
 */
@Service
@Slf4j
@Transactional
public class OrderBasicServiceImpl extends ServiceImpl<OrderBasicMapper, OrderBasic> implements OrderBasicService {

    @Autowired
    private OrderBasicMapper orderBasicMapper;

    @Autowired
    private UserBasicService userBasicService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private MoneyTotalService moneyTotalService;
    /**
     * 添加订单
     *
     * @param orderBasic
     */
    @Override
    public Boolean InsertOrderBasic(OrderBasic orderBasic) {
        if (orderBasic == null) return false;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        orderBasic.setUserId(userId);
        orderBasic.setNumber(IdWorker.getId() + "");
        orderBasic.setOrderStatus(1);
        orderBasic.setCreateUser(userId);
        orderBasic.setCreateDate(LocalDateTime.now());
        orderBasic.setUpdateUser(userId);
        orderBasic.setUpdateDate(LocalDateTime.now());
        MoneyTotal moneyTotal = new MoneyTotal();
        moneyTotal.setMoney(orderBasic.getTotalMoney());
        moneyTotalService.UpdateMoney(moneyTotal);
        orderBasic.setIsDeleted(false);
        return orderBasicMapper.insert(orderBasic) > 0;
    }

    /**
     * 根据条件(订单号模糊查询，订单状态精确查询,下单时间范围查询)分页查询
     *
     * @param searchOrderVo
     * @param pageIndex
     * @param pageSize
     */
    @Override
    public PageResponseDto<List<OrderBasicDto>> SelectOrderPageByConditions(SearchOrderVo searchOrderVo, int pageIndex, int pageSize) {
        LambdaQueryWrapper<OrderBasic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(searchOrderVo.getNumber()),OrderBasic::getNumber,searchOrderVo.getNumber())
                .eq(searchOrderVo.getOrderStatus() != 0,OrderBasic::getOrderStatus,searchOrderVo.getOrderStatus())
                .orderByDesc(OrderBasic::getCreateDate);
        List<OrderBasic> orderBasics = orderBasicMapper.selectList(queryWrapper);
        List<OrderBasicDto> orderBasicDtos = new ArrayList<>();
        OrderBasicDto orderBasicDto = null;
        for (OrderBasic orderBasic : orderBasics) {
            orderBasicDto = this.SelectOrderById(orderBasic.getId());
            orderBasicDtos.add(orderBasicDto);
        }
        PageResponseDto<List<OrderBasicDto>> pageResponseDto = new PageResponseDto<>();
        pageResponseDto.setSize(orderBasicDtos.size());
        pageResponseDto.setData(new PageUtils<OrderBasicDto>().page(orderBasicDtos,pageIndex,pageSize));
        return pageResponseDto;
    }

    /**
     * 根据id修改订单状态
     *
     * @param orderBasic
     */
    @Override
    public Boolean UpdateOrderBasicById(OrderBasic orderBasic) {
        if (orderBasic == null) return false;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        OrderBasic order = orderBasicMapper.selectById(orderBasic.getId());
        order.setOrderStatus(orderBasic.getOrderStatus());
        order.setUpdateUser(userId);
        order.setUpdateDate(LocalDateTime.now());
        return orderBasicMapper.updateById(order) > 0;
    }

    /**
     * 根据id将数据库原始数据转化为详情数据
     * @param id
     * @return
     */
    public OrderBasicDto SelectOrderById(int id) {
        OrderBasic orderBasic = orderBasicMapper.selectById(id);
        int userId = orderBasic.getUserId();
        int addressId = orderBasic.getAddressId();
        int CreateId = orderBasic.getCreateUser();
        int UpdateId = orderBasic.getUpdateUser();
        OrderBasicDto orderBasicDto = new OrderBasicDto();
        BeanUtils.copyProperties(orderBasic,orderBasicDto);
        orderBasicDto.setUserBasicDto(userBasicService.SelectUserById(userId));
        orderBasicDto.setAddressBook(addressBookService.SelectAddressBookById(addressId));
        orderBasicDto.setCreateUser(userBasicService.SelectUserById(CreateId).getUsername());
        orderBasicDto.setUpdateUser(userBasicService.SelectUserById(UpdateId).getUsername());
        return orderBasicDto;
    }
}
